/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.cncqs.backend.company.web;

import com.cncqs.backend.company.entity.CheckDTO;
import com.cncqs.backend.company.entity.ComCompany;
import com.cncqs.backend.company.service.ComCompanyService;
import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.collect.MapUtils;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.sys.entity.Office;
import com.jeesite.modules.sys.utils.EmpUtils;
import com.jeesite.modules.test.entity.TestTree;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * 企业管理Controller
 * @author cxy
 * @version 2019-04-16
 */
@Controller
@RequestMapping(value = "${adminPath}/company/comCompany")
public class ComCompanyController extends BaseController {

	@Autowired
	private ComCompanyService comCompanyService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ComCompany get(Long id, boolean isNewRecord) {
		return comCompanyService.get(id == null ? "" : id.toString(), isNewRecord);
	}

	@RequiresPermissions({"company:comCompany:view"})
	@RequestMapping({"index"})
	public String index(ComCompany comCompany, Model model) {
		return "backend/company/comCompanyIndex";
	}

	@RequiresPermissions({"company:comCompany:view"})
	@RequestMapping({"checkIndex"})
	public String checkIndex(ComCompany comCompany, Model model) {
		return "backend/company/comCompanyCheckIndex";
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("company:comCompany:view")
	@RequestMapping(value = {"list", ""})
	public String list(ComCompany comCompany, Model model) {
		model.addAttribute("comCompany", comCompany);
		return "backend/company/comCompanyList";
	}

	/**
	 * 审核列表
	 */
	@RequiresPermissions("company:comCompany:view")
	@RequestMapping(value = {"checkList", ""})
	public String checkList(ComCompany comCompany, Model model) {
		model.addAttribute("comCompany", comCompany);
		return "backend/company/comCompanyCheckList";
	}



	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("company:comCompany:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<ComCompany> listData(ComCompany comCompany, HttpServletRequest request, HttpServletResponse response) {

		if (org.apache.commons.lang3.StringUtils.isBlank(comCompany.getEmployee().getOffice().toString()) && EmpUtils.getOffice() != null) {
			Office office = new Office();
			office.setOfficeCode(EmpUtils.getOffice().getOfficeCode());
			comCompany.getEmployee().setOffice(office);
		}
		comCompany.getEmployee().getOffice().setIsQueryChildren(Boolean.valueOf(true));
		comCompany.getEmployee().getCompany().setIsQueryChildren(Boolean.valueOf(true));

		comCompany.setPage(new Page<>(request, response));
		Page<ComCompany> page = comCompanyService.findPage(comCompany);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("company:comCompany:view")
	@RequestMapping(value = "form")
	public String form(ComCompany comCompany, Model model) {
		model.addAttribute("comCompany", comCompany);
		return "backend/company/comCompanyForm";
	}


	/**
	 * 保存企业
	 */
	@RequiresPermissions("company:comCompany:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated ComCompany comCompany) {
		comCompanyService.save(comCompany);
		return renderResult(Global.TRUE, text("保存企业成功！"));
	}
	
	/**
	 * 停用企业
	 */
	@RequiresPermissions("company:comCompany:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(ComCompany comCompany) {
		comCompany.setStatus(ComCompany.STATUS_DISABLE);
		comCompanyService.updateStatus(comCompany);
		return renderResult(Global.TRUE, text("停用企业成功"));
	}
	
	/**
	 * 启用企业
	 */
	@RequiresPermissions("company:comCompany:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(ComCompany comCompany) {
		comCompany.setStatus(ComCompany.STATUS_NORMAL);
		comCompanyService.updateStatus(comCompany);
		return renderResult(Global.TRUE, text("启用企业成功"));
	}
	
	/**
	 * 删除企业
	 */
	@RequiresPermissions("company:comCompany:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(ComCompany comCompany) {
		comCompanyService.delete(comCompany);
		return renderResult(Global.TRUE, text("删除企业成功！"));
	}


	/**
	 * 审核公司页面
	 */
	@RequiresPermissions("company:comCompany:view")
	@RequestMapping(value = "checkPage")
	public String checkPage(ComCompany comCompany, Model model) {

		List<CheckDTO> checkDTOList = comCompanyService.queryCheckDetailByCompanyId(Long.parseLong(comCompany.getId()));
		StringBuffer record = new StringBuffer();
		if (checkDTOList != null && checkDTOList.size() > 0) {
			for (int i = 0; i < checkDTOList.size(); i++) {
				CheckDTO checkDTO = checkDTOList.get(i);
				String dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(checkDTO.getTime());
				record.append(dateFormat).append(" ").append(checkDTO.getCreateBy()).append(" ")
						.append(checkDTO.getCheckStatus() == 1 ? "合格通过；原因：" : "未通过；原因：").append(checkDTO.getCheckReason()).append("\n");
			}
		} else {
			record.append("暂无审核操作记录");
		}
		comCompany.setCheckRecord(record);
		model.addAttribute("comCompany", comCompany);
		return "backend/company/comCompanyCheck";
	}


	/**
	 * 审核企业提交
	 */
	@RequiresPermissions("company:comCompany:edit")
	@PostMapping(value = "saveCheck")
	@ResponseBody
	public String saveCheck(@Validated ComCompany comCompany) {
		comCompanyService.saveCheck(comCompany);
		return renderResult(Global.TRUE, text("审核处理成功！"));
	}

	@RequestMapping("isWorkDay")
	@ResponseBody
	public Long isWorkDay() {
		return comCompanyService.getIsWorkDayCodeByDate();
	}

	/**
	 * 获取树结构数据
	 * @param excludeCode 排除的Code
	 * @param isShowCode 是否显示编码（true or 1：显示在左侧；2：显示在右侧；false or null：不显示）
	 * @return
	 */
	@RequiresPermissions("company:comCompany:view")
	@RequestMapping(value = "treeData")
	@ResponseBody
	public List<Map<String, Object>> treeData(String excludeCode, String isShowCode) {
		List<Map<String, Object>> mapList = ListUtils.newArrayList();
		ComCompany comCompany = new ComCompany();
		if (EmpUtils.getOffice() != null) {
			Office office = new Office();
			office.setOfficeCode(EmpUtils.getOffice().getOfficeCode());
			comCompany.getEmployee().setOffice(office);
		}
		comCompany.getEmployee().getOffice().setIsQueryChildren(Boolean.valueOf(true));
		comCompany.getEmployee().getCompany().setIsQueryChildren(Boolean.valueOf(true));
		List<ComCompany> list = comCompanyService.findList(comCompany);
		for (int i=0; i<list.size(); i++){
			ComCompany e = list.get(i);
			Map<String, Object> map = MapUtils.newHashMap();
			map.put("id", e.getId());

			map.put("name", StringUtils.getTreeNodeName(isShowCode, e.getId().toString(), e.getName()));
			mapList.add(map);
		}
		return mapList;
	}

}